* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file calculates the degree of product overlap by acquirer and target product scope in Appendix Table A.2.

* From Do File #P8b
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_com_product_cn8plus.dta", clear

* Ratio of same after-merger products to number of products of target before merger (i.e., how many of target's products continued after merger)
gen share_nsp_afterT = nsp_after/nprodT if nprod_before>0 & nprodT>0 & nprod_after>0
sum share_nsp_afterT

egen rankA = rank(vrdA_before), by(cvrnrA aar) field
gen rankA1 = (rankA==1)

egen rankT = rank(vrdT_before), by(cvrnrA aar) field
gen rankT1 = (rankT==1)
egen sumrankT1 = sum(rankT1), by(cvrnrA aar)

gen samecore = (rankA==1 & rankT==1)
gen overlap = (vrdA_before!=. & vrdT_before!=.)

collapse (mean) nsp_before nsp_after nprod_before nprod_after nsp_afterN nprodT nprod_cont nprod_sp_cont vrdT_sp vrdT vrdA_sp_before vrdA_sp_after vrdA_cont_before vrdA_cont_after vrdT_cont (sum) vrdA_before vrdA_after (max) samecore overlap, by(cvrnrA aar cvrnrT)

foreach x in nsp_before nsp_after nprod_before nprod_after nprodT nprod_cont nprod_sp_cont vrdT_sp vrdT vrdA_sp_before vrdA_sp_after vrdA_cont_before vrdA_cont_after vrdT_cont vrdA_before vrdA_after {
	replace `x' = 0 if `x'==.
}

label var nsp_before "# of same prod as pre-merger target before merger"
label var nsp_after "# of same prod as pre-merger target after merger"
label var nprod_before "# of prod of acquirer before merger"
label var nprod_after "# of prod of acquirer after merger"
label var nprod_cont "# of prod continued by acquirer before and after merger"
label var nprod_sp_cont "# of same prod as pre-merger target continued by acquirer before and after merger"
label var nprodT "# of prod of target before merger"

order cvrnrA aar cvrnrT nsp_before nsp_after nprod_before nprod_after nsp_afterN vrdA_before vrdA_after vrdT_sp nprodT vrdT nprod_cont nprod_sp_cont vrdA_sp_before vrdA_sp_after vrdA_cont_before vrdA_cont_after vrdT_cont
sum

* Ratio of same pre-merger products to unique number of products of acquirer before merger (i.e., overlap from acquirer's perspective)
gen share_nsp_beforeA = nsp_before/nprod_before if nprod_before>0 & nprodT>0
sum share_nsp_beforeA

* Ratio of same pre-merger products to unique number of products of target before merger (i.e., overlap from target's perspective)
gen share_nsp_beforeT = nsp_before/nprodT if nprod_before>0 & nprodT>0
sum share_nsp_beforeT

* Ratio of same pre-merger products to unique number of products of acquirer + target before merger (i.e., overlap from combined's perspective)
gen share_nsp_before = nsp_before/(nprod_before + nprodT - nsp_before) if nprod_before>0 & nprodT>0
sum share_nsp_before

sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before==1 & nprodT==1
local mean1 = `r(mean)'
local N1 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=2 & nprod_before<=4 & nprodT==1
local mean2 = `r(mean)'
local N2 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=5 & nprodT==1
local mean3 = `r(mean)'
local N3 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprodT==1
local mean4 = `r(mean)'
local N4 = `r(N)'

sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before==1 & nprodT>=2 & nprodT<=4
local mean5 = `r(mean)'
local N5 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=2 & nprod_before<=4 & nprodT>=2 & nprodT<=4
local mean6 = `r(mean)'
local N6 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=5 & nprodT>=2 & nprodT<=4
local mean7 = `r(mean)'
local N7 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprodT>=2 & nprodT<=4
local mean8 = `r(mean)'
local N8 = `r(N)'

sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before==1 & nprodT>=5
local mean9 = `r(mean)'
local N9 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=2 & nprod_before<=4 & nprodT>=5
local mean10 = `r(mean)'
local N10 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=5 & nprodT>=5
local mean11 = `r(mean)'
local N11 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprodT>=5
local mean12 = `r(mean)'
local N12 = `r(N)'

sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before==1
local mean13 = `r(mean)'
local N13 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=2 & nprod_before<=4
local mean14 = `r(mean)'
local N14 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0 & nprod_before>=5
local mean15 = `r(mean)'
local N15 = `r(N)'
sum share_nsp_before if nprod_before>0 & nprodT>0
local mean16 = `r(mean)'
local N16 = `r(N)'

preserve

clear
set obs 4
gen acquirer = ""
replace acquirer = "1" if _n==1
replace acquirer = "2 to 4" if _n==2
replace acquirer = "5+" if _n==3
replace acquirer = "All" if _n==4
forval i = 1/3{
	gen tgt`i' = .
	forval j = 1/4{
		local k = (`i'-1)*4 + `j'
		replace tgt`i' = `mean`k'' if _n==`j'
	}
}
gen tgtAll = .
replace tgtAll = `mean13' if _n==1
replace tgtAll = `mean14' if _n==2
replace tgtAll = `mean15' if _n==3
replace tgtAll = `mean16' if _n==4

log using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A2.log", replace
* By number of products
list
log close

restore
xtile vrdA_before_xt = vrdA_before if nprod_before>0 & nprodT>0, nq(3)
xtile vrdT_xt = vrdT if nprod_before>0 & nprodT>0, nq(3)

forval i = 1/3{
	forval j = 1/3{
		local k = (`i'-1)*4 + `j'
		sum share_nsp_before if nprod_before>0 & nprodT>0 & vrdA_before_xt==`j' & vrdT_xt==`i'
		local mean`k' = `r(mean)'
		local N`k' = `r(N)'
 		}
	sum share_nsp_before if nprod_before>0 & nprodT>0 & vrdT_xt==`i'
	local l = (`i'-1)*4 + 4
	local mean`l' = `r(mean)'
	local N`l' = `r(N)'
}

forval j = 1/3{
	local k = 12 + `j'
	sum share_nsp_before if nprod_before>0 & nprodT>0 & vrdA_before_xt==`j'
	local mean`k' = `r(mean)'
	local N`k' = `r(N)'
	}
sum share_nsp_before if nprod_before>0 & nprodT>0
local mean16 = `r(mean)'
local N16 = `r(N)'

preserve

clear
set obs 4
gen acquirer = ""
replace acquirer = "1" if _n==1
replace acquirer = "2" if _n==2
replace acquirer = "3" if _n==3
replace acquirer = "All" if _n==4

forval i = 1/3{
	gen tgt`i' = .
	forval j = 1/4{
		local k = (`i'-1)*4 + `j'
		replace tgt`i' = `mean`k'' if _n==`j'
	}
}
gen tgtAll = .
replace tgtAll = `mean13' if _n==1
replace tgtAll = `mean14' if _n==2
replace tgtAll = `mean15' if _n==3
replace tgtAll = `mean16' if _n==4

log using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_A3.log", replace
* By sales
list
log close
